home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / security / Signer.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  4.6 KB  |  171 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)Signer.java    1.34 98/09/24
  3.  *
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.security;
  16.  
  17. import java.io.*;
  18.  
  19. /**
  20.  * This class is used to represent an Identity that can also digitally
  21.  * sign data.
  22.  *
  23.  * <p>The management of a signer's private keys is an important and
  24.  * sensitive issue that should be handled by subclasses as appropriate
  25.  * to their intended use.
  26.  *
  27.  * @see Identity
  28.  *
  29.  * @version 1.34 99/03/26
  30.  * @author Benjamin Renaud
  31.  *
  32.  * @deprecated This class is no longer used. Its functionality has been
  33.  * replaced by <code>java.security.KeyStore</code>, the
  34.  * <code>java.security.cert</code> package, and
  35.  * <code>java.security.Principal</code>.
  36.  */
  37. public abstract class Signer extends Identity {
  38.  
  39.     /**
  40.      * The signer's private key.
  41.      *
  42.      * @serial
  43.      */
  44.     private PrivateKey privateKey;
  45.  
  46.     /**
  47.      * Creates a signer. This constructor should only be used for
  48.      * serialization.
  49.      */
  50.     protected Signer() {
  51.     super();
  52.     }
  53.  
  54.  
  55.     /**
  56.      * Creates a signer with the specified identity name.
  57.      *
  58.      * @param name the identity name.
  59.      */
  60.     public Signer(String name) {
  61.     super(name);
  62.     }
  63.  
  64.     /**
  65.      * Creates a signer with the specified identity name and scope.
  66.      *
  67.      * @param name the identity name.
  68.      *
  69.      * @param scope the scope of the identity.
  70.      *
  71.      * @exception KeyManagementException if there is already an identity
  72.      * with the same name in the scope.
  73.      */
  74.     public Signer(String name, IdentityScope scope)
  75.     throws KeyManagementException {
  76.     super(name, scope);
  77.     }
  78.  
  79.     /**
  80.      * Returns this signer's private key.
  81.      *
  82.      * <p>First, if there is a security manager, its <code>checkSecurityAccess</code> 
  83.      * method is called with <code>"getSignerPrivateKey"</code> 
  84.      * as its argument to see if it's ok to return the private key. 
  85.      * 
  86.      * @return this signer's private key, or null if the private key has
  87.      * not yet been set.
  88.      * 
  89.      * @exception  SecurityException  if a security manager exists and its  
  90.      * <code>checkSecurityAccess</code> method doesn't allow 
  91.      * returning the private key.
  92.      * 
  93.      * @see SecurityManager#checkSecurityAccess
  94.      */
  95.     public PrivateKey getPrivateKey() {
  96.     check("getSignerPrivateKey");
  97.     return privateKey;
  98.     }
  99.  
  100.    /**
  101.      * Sets the key pair (public key and private key) for this signer.
  102.      *
  103.      * <p>First, if there is a security manager, its <code>checkSecurityAccess</code> 
  104.      * method is called with <code>"setSignerKeyPair"</code> 
  105.      * as its argument to see if it's ok to set the key pair. 
  106.      * 
  107.      * @param pair an initialized key pair.
  108.      *
  109.      * @exception InvalidParameterException if the key pair is not
  110.      * properly initialized.
  111.      * @exception KeyException if the key pair cannot be set for any
  112.      * other reason.
  113.      * @exception  SecurityException  if a security manager exists and its  
  114.      * <code>checkSecurityAccess</code> method doesn't allow 
  115.      * setting the key pair.
  116.      * 
  117.      * @see SecurityManager#checkSecurityAccess
  118.      */
  119.     public final void setKeyPair(KeyPair pair)
  120.     throws InvalidParameterException, KeyException {
  121.     check("setSignerKeyPair");
  122.     final PublicKey pub = pair.getPublic();
  123.     PrivateKey priv = pair.getPrivate();
  124.  
  125.     if (pub == null || priv == null) {
  126.         throw new InvalidParameterException();
  127.     }
  128.     try {
  129.         AccessController.doPrivileged(new PrivilegedExceptionAction() {
  130.         public Object run() throws KeyManagementException {
  131.             setPublicKey(pub);
  132.             return null;
  133.         }
  134.         });
  135.     } catch (PrivilegedActionException pae) {
  136.         throw (KeyManagementException) pae.getException();
  137.     }
  138.     privateKey = priv;
  139.     }
  140.  
  141.     String printKeys() {
  142.     String keys = "";
  143.     PublicKey publicKey = getPublicKey();
  144.     if (publicKey != null && privateKey != null) {
  145.         keys = "\tpublic and private keys initialized";
  146.  
  147.     } else {
  148.         keys = "\tno keys";
  149.     }
  150.     return keys;
  151.     }
  152.  
  153.     /**
  154.      * Returns a string of information about the signer.
  155.      *
  156.      * @return a string of information about the signer.
  157.      */
  158.     public String toString() {
  159.     return "[Signer]" + super.toString();
  160.     }
  161.  
  162.     private static void check(String directive) {
  163.     SecurityManager security = System.getSecurityManager();
  164.     if (security != null) {
  165.         security.checkSecurityAccess(directive);
  166.     }
  167.     }
  168.  
  169. }
  170.  
  171.